package com.ll.wechat.friends.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ll.wechat.R;
import com.ll.wechat.base.activity.BaseActivity;
import com.ll.wechat.base.app.Constant;
import com.ll.wechat.base.net.NetCallBackListener;
import com.ll.wechat.base.net.NetUtil;
import com.ll.wechat.base.utils.widget.MyToast;
import com.ll.wechat.chat.activity.ChatActivity;
import com.ll.wechat.database.DataBaseHelperUtil;

/**
 * 好友的个人信息展示
 * 
 * @ClassName: FriendsInfoActivity
 * @author yeliangliang
 * @date 2015-7-28 下午6:20:17
 */
public class FriendsInfoActivity extends BaseActivity implements OnClickListener {
	private PopupWindow popupWindow ;
	private View view1 = null ;
	private TextView tv_name;
	private TextView tv_post_message;// 发消息
	private String name = "";// 用户昵称
	private String passName = "";// 用户名
	private DataBaseHelperUtil dataBaseHelperUtil;

	@Override
	public void initView() {
		setContentView(R.layout.activity_friends_info);
		tv_name = (TextView) findViewById(R.id.tv_friends_name);
		tv_post_message = (TextView) findViewById(R.id.tv_friends_post_message);
		tv_post_message.setOnClickListener(this);
		// 取消actionBar的图标
		getActionBar().setDisplayShowHomeEnabled(false);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		setTitle("详细信息");
	}

	@Override
	public void initData() {
		Intent intent = getIntent();
		name = intent.getStringExtra("name");
		passName = intent.getStringExtra("passName");
		tv_name.setText(name);
	}

	// 监听返回键，防止内存泄露
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			finishActivity(FriendsInfoActivity.this);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_friends_post_message:// 跳转到聊天界面
			Bundle bundle = new Bundle();
			bundle.putString("name", name);
			bundle.putString("passName", passName);
			showActivity(FriendsInfoActivity.this, ChatActivity.class, bundle);
			break;
		case 0001://删除好友
			deleteFriend();
			break;
		default:
			break;
		}
	}

	/**
	 * 创建ActionBar菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.friendsinfo, menu);
		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * ActionBar的点击监听
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finishActivity(this);
			return true;
		case R.id.action_more:
			initPopWindow();
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * 删除好友
	 * 
	 * @author yeliangliang
	 * @date 2015-8-21 下午5:40:20
	 * @version V1.0
	 * @return void
	 */
	private void deleteFriend() {
		if (!NetUtil.isNetConnection()) {
			MyToast.showToast(FriendsInfoActivity.this, "请检查网络连接");
			return ;
		}
		NetUtil.deleteFriend(FriendsInfoActivity.this, passName, new NetCallBackListener() {
			
			@Override
			public void responseSuccess(int i, Object o) {
				handler.obtainMessage(i, null).sendToTarget();
			}
			
			@Override
			public void responseFailed(int i) {
				handler.obtainMessage(i).sendToTarget();
			}
			
			@Override
			public void responseError(int i) {
				
			}
		});
		
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			super.handleMessage(msg);
			switch (msg.what) {
			case Constant.REQUEST_SUCCESS:
				// 发送广播--更新好友列表
				Intent intent = new Intent(Constant.RECIVER_DELETE_FRIENDS);
				sendBroadcast(intent, "com.wechat.permission.RECIVER_MESSAGE");
				// 发送广播--更新聊天记录

				dataBaseHelperUtil = DataBaseHelperUtil.getInstance(FriendsInfoActivity.this);
				dataBaseHelperUtil.deleteRecordByName(name);
				Intent intent2 = new Intent(Constant.RECIVER_MESSAGE);
				sendBroadcast(intent2, "com.wechat.permission.RECIVER_MESSAGE");
				MyToast.showToast(FriendsInfoActivity.this, "删除成功");
				finishActivity(FriendsInfoActivity.this);
				break;
			case Constant.REQUEST_FAILD:
				MyToast.showToast(FriendsInfoActivity.this, "删除失败");
				break;
			default:
				break;
			}
		}

	};
	
	private void initPopWindow() {
		if (popupWindow==null) {
			View view = LayoutInflater.from(this).inflate(R.layout.layout_popwindow_actionbar, null);
			view.setId(0001);
			view.setOnClickListener(this);
			ImageView uImageView = (ImageView) view.findViewById(R.id.img_delete);
			uImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.icon_info_delete));
			TextView tv = (TextView) view.findViewById(R.id.tv_delete);
			tv.setText("删除");
			popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			view1 = LayoutInflater.from(this).inflate(R.layout.activity_friends_info, null);
			popupWindow.setOutsideTouchable(true);
			popupWindow.setBackgroundDrawable(new ColorDrawable());
		}
		if (popupWindow.isShowing()) {
			return ;
		}
		popupWindow.showAtLocation(view1, Gravity.TOP | Gravity.RIGHT, 0, 150);
	}
}
